import { queryLinkComment, removeCommentById } from "@/api/content/comment";
import { useState, useCallback, useEffect } from "react";
import { type Comment } from "@/types/content/comment";
import { message } from "antd";

export const useLinkComment = () => {
    const [messageApi] = message.useMessage();
    const [loading, setLoading] = useState(false);
    const [pagination, setPagination] = useState<Pagination>({
        pageNo: 1,
        pageSize: 10,
        total: 0,
    });
    const [tableData, setTableData] = useState<Comment[]>([]);
    /**
     * 获取标签列表
     */
    const getTableList = useCallback(async () => {
        const params = {
            // 页码
            pageNo: pagination.pageNo || 1,
            pageSize: pagination.pageSize || 10,
        };
        setLoading(true);
        const res = await queryLinkComment(params);
        setLoading(false);
        if (res.code === 200) {
            setTableData(res.data?.rows || []);
            setPagination({
                pageNo: pagination.pageNo,
                pageSize: pagination.pageSize,
                total: res.data.total
            })

        }
    }, [pagination.pageNo, pagination.pageSize]);
        /**
   * 删除评论
   * @param id 
   */
        const removeComment = async (row: Comment) => {
            setLoading(true)
            const res = await removeCommentById(row.id)
            setLoading(false)
            if (res.code === 200) {
                messageApi.success('删除成功')
                if (tableData.length === 1) {
                    setPagination({
                        pageNo: Math.max(1, pagination.pageNo - 1),
                        pageSize: pagination.pageSize,
                        total: pagination.total
                    })
                } else {
                    getTableList()
                }
            }
        }
    useEffect(() => {
        getTableList();
    }, [getTableList]);
    return {
        loading,
        tableData,
        pagination,
        setPagination,
        removeComment
    }
}